package com.lanyam.travel.base.service.impl;


import com.lanyam.travel.base.entity.OrderRefund;
import com.lanyam.travel.base.entity.OrderRefundDetails;
import com.lanyam.travel.base.entity.User;
import com.lanyam.travel.base.entity.vo.OrderRefundVo;
import com.lanyam.travel.base.entity.vo.TravelOrderVo;
import com.lanyam.travel.base.dao.OrderRefundMapper;
import com.lanyam.travel.base.service.IOrderRefundDetailsService;
import com.lanyam.travel.base.service.IOrderRefundService;
import com.lanyam.travel.base.service.UserService;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;


@Service
@Transactional
@Slf4j
public class OrderRefundServiceImpl implements IOrderRefundService {

    @Resource
    private OrderRefundMapper orderRefundMapper;
    
    @Autowired
    private IOrderRefundDetailsService orderRefundDetailService;
    
    @Autowired
    private UserService userService;

    @Override
    public IPage<OrderRefundVo> getPageOrderRefund (Map<String, Object> queryMap, Integer page, Integer rows) {
        Page<OrderRefundVo> pageSize = new Page<OrderRefundVo>(page, rows);
        Page<OrderRefundVo> pageInfo = orderRefundMapper.getPage(pageSize, queryMap);
        List<OrderRefundVo> records = pageInfo.getRecords();
        for (OrderRefundVo orderRefundVo : records) {
        	OrderRefundDetails detail = orderRefundDetailService.getLatestRefundDeatail(orderRefundVo.getId());
        	if(null != detail)
        	{
        		orderRefundVo.setReason(detail.getReason());
        		orderRefundVo.setRemarks(detail.getRemarks());
        		orderRefundVo.setImg(detail.getImgUrl());
        		orderRefundVo.setVerifyFailReason(detail.getVerifyFailReason());
        	}
		}
        return pageInfo;
    }
    
    @Override
	public Page<OrderRefundVo> queryOrderRefundByPage(Integer pageNum, Integer pageSize, Map<String, Object> reqMap) {
		// TODO Auto-generated method stub
    	User user = userService.getCurrentUser();
    	if(!StringUtils.isEmpty(user.getSupplyId()))
    	{
			//供应商
    		reqMap.put("supplyId", user.getSupplyId());
		}
    	Page<OrderRefundVo> ipage = new Page<OrderRefundVo>(pageNum, pageSize);
        Page<OrderRefundVo> pageInfo = orderRefundMapper.getPage(ipage, reqMap);
        List<OrderRefundVo> records = pageInfo.getRecords();
        for (OrderRefundVo orderRefundVo : records) {
        	OrderRefundDetails detail = orderRefundDetailService.getLatestRefundDeatail(orderRefundVo.getOrderId());
        	orderRefundVo.setReason(detail.getReason());
        	orderRefundVo.setRemarks(detail.getRemarks());
		}
        return pageInfo;
	}
    
    @Override
    public void addOrderRefund(OrderRefund orderRefund) {
        orderRefundMapper.insert(orderRefund);
    }

    @Override
    public void updateOrderRefund(OrderRefund orderRefund) {
        orderRefundMapper.updateById(orderRefund);
    }

    @Override
    public void delById(String id) {
        orderRefundMapper.delById(id);
    }

	@Override
	public OrderRefund queryOrderRefundByOrderId(String orderId) {
		// TODO Auto-generated method stub
		QueryWrapper<OrderRefund> wrapper = new QueryWrapper<OrderRefund>();
		wrapper.eq("order_id", orderId);
		OrderRefund refund = orderRefundMapper.selectOne(wrapper);
		return refund;
	}

	@Override
	public OrderRefund getOrderRefundById(String id) {
		// TODO Auto-generated method stub
		OrderRefund refund = orderRefundMapper.selectById(id);
		return refund;
	}

	
}